package com.one.consumer.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.common.login.presenter.LoginPresenter;
import com.one.common.common.login.view.SendCodeView;
import com.one.common.config.RouterPath;
import com.one.common.config.VerifyCodeType;
import com.one.common.utils.StringUtils;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * author zhulunjun
 * email  zhulunjun@qq.com
 * date   2018/8/4 下午4:30
 **/
@Route(path = RouterPath.REGISTER)
public class RegisterActivity extends BaseActivityC<LoginPresenter> implements SendCodeView {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_btn_code)
    TextView tvBtnCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_btn_register)
    TextView tvBtnRegister;
    @BindView(R.id.et_c_pwd)
    EditText etCPwd;

    @Override
    protected void initPresenter() {
        mPresenter = new LoginPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText(StringUtils.format(R.string.register));
    }

    @OnClick(R.id.tv_btn_code)
    public void getCode() {
        startCountDown();
    }

    @OnClick(R.id.tv_btn_register)
    public void register() {
        mPresenter.register(etPhone.getText().toString(),
                etPwd.getText().toString(), "1",
                etCode.getText().toString(),
                etCPwd.getText().toString());
    }

    /**
     * 开始倒计时
     */
    @SuppressLint("CheckResult")
    public void startCountDown() {
        if (mPresenter.checkDataPhone(etPhone.getText().toString())) {
            return;
        }
        etCode.setText("");
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(COUNT_DOWN_SECOND + 1)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        RxView.enabled(tvBtnCode).accept(false);
                        mPresenter.sendCode(etPhone.getText().toString(), VerifyCodeType.REGISTER);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        tvBtnCode.setText(COUNT_DOWN_SECOND - aLong + "s");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        resetCountDown();   // 重置倒计时
                    }
                });
    }

    /**
     * 重置倒计时
     */
    public void resetCountDown() {
        try {
            RxView.enabled(tvBtnCode).accept(true);
            tvBtnCode.setText("重新获取");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
